package mms

class InspectionLogController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 20, 100)

        params.sort = "id"
        params.order ="desc"

        [inspectionLogInstanceList: InspectionLog.list(params),
            inspectionLogInstanceTotal: InspectionLog.count()]
    }

    def create = {
        def inspectionLogInstance = new InspectionLog()
        inspectionLogInstance.properties = params
        return [inspectionLogInstance: inspectionLogInstance]
    }

    def save = {
        def inspectionLogInstance = new InspectionLog(params)
        if (inspectionLogInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'inspectionLog.label', default: 'InspectionLog'), inspectionLogInstance])}"
            redirect(action: "show", id: inspectionLogInstance.id)
        }
        else {
            render(view: "create", model: [inspectionLogInstance: inspectionLogInstance])
        }
    }

    def show = {
        def inspectionLogInstance = InspectionLog.get(params.id)
        if (!inspectionLogInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'inspectionLog.label', default: 'InspectionLog'), params.id])}"
            redirect(action: "list")
        }
        else {
            [inspectionLogInstance: inspectionLogInstance]
        }
    }

    def edit = {
        def inspectionLogInstance = InspectionLog.get(params.id)
        if (!inspectionLogInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'inspectionLog.label', default: 'InspectionLog'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [inspectionLogInstance: inspectionLogInstance]
        }
    }

    def update = {
        def inspectionLogInstance = InspectionLog.get(params.id)
        if (inspectionLogInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (inspectionLogInstance.version > version) {
                    
                    inspectionLogInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'inspectionLog.label', default: 'InspectionLog')] as Object[], "Another user has updated this InspectionLog while you were editing")
                    render(view: "edit", model: [inspectionLogInstance: inspectionLogInstance])
                    return
                }
            }
            inspectionLogInstance.properties = params
            if (!inspectionLogInstance.hasErrors() && inspectionLogInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'inspectionLog.label', default: 'InspectionLog'), inspectionLogInstance])}"
                redirect(action: "show", id: inspectionLogInstance.id)
            }
            else {
                render(view: "edit", model: [inspectionLogInstance: inspectionLogInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'inspectionLog.label', default: 'InspectionLog'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def inspectionLogInstance = InspectionLog.get(params.id)
        if (inspectionLogInstance) {
            try {
                inspectionLogInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'inspectionLog.label', default: 'InspectionLog'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'inspectionLog.label', default: 'InspectionLog'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'inspectionLog.label', default: 'InspectionLog'), params.id])}"
            redirect(action: "list")
        }
    }

    def bubble = {
        def company = Company.get(params.id)
  
        if (company) {
              params.max = Math.min(params.max ? params.int('max') : 20, 100)

                params.sort = "id"
                params.order ="desc"

                return  [inspectionLogInstanceList: InspectionLog.findAllByCompany(company ,params),
                    inspectionLogInstanceTotal: InspectionLog.countByCompany(company)]
        }
        render("没有这个企业。")
    }
}
